package o2o.web.actions.postman;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.company.postman.PostTypeMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.config.dao.utils.DaoUtils;
import o2o.config.dao.utils.Expression;
import o2o.config.dao.utils.QueryCondition;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.company.postman.POST_TYPE_OPEN_STATE;
import o2o.dao.PO.company.postman.PostTypeInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.company.postman.PostTypeInfoVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2019/7/30 15:27
 * @Classname: PostmanTypeController
 * @To 骑手类型管理设置
 */
@RestController
public class PostmanTypeController {


    // 系统添加一个骑手类型
    @RequestMapping(value = "/post/type/add")
    public String postTypeAdd(String token, PostTypeInfo info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (info.getType() == null) {
            return AjaxResult.errorResult("类型不能为空");
        }
        if (info.getName() == null) {
            return AjaxResult.errorResult("名字不能空");
        }
        // 查询当前是不是已经有了
        QueryCondition query = new QueryCondition();
        query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("type", info.getType()));
        String sql = DaoUtils.getSql(PojoTables.POST_TYPE, query);
        long count = 0;
        try {
            count = UserDaoSql.getInstance().getCount(sql);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("操作失败,请稍后重试");
        }
        if (count > 0) {
            return AjaxResult.errorResult("该类型已经添加");
        }
        Long cTime = DateUtils.getCurrTime();
        info.setCreateTime(cTime).setCreateUser(user.getUsername()).setDelFlag(0).setId(null);
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.POST_TYPE, info);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("操作失败,请稍后重试");
        }
        // cache
        PostTypeMemory.getInstance().add(cTime);
        // 数据通讯
        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POST_TYPE_INFO_ADD).setLong01(cTime);
        JmsSender.setTopicSender(notify);

        return AjaxResult.successResult();
    }

    // 更新-删除
    @RequestMapping(value = "/post/type/modify-del")
    public String postTypeModify(String token, PostTypeInfo info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (info.getId() == null) {
            return AjaxResult.errorResult("id不能为空");
        }
        PostTypeInfo infoDB = PostTypeMemory.getInstance().getById(info.getId());
        if (infoDB == null) {
            return AjaxResult.errorResult("未获取到信息,请核实");
        }
        // 删除
        if (info.getDelFlag() == 1) {
            StringBuilder sql = new StringBuilder();
            sql.append("update  ").append(PojoTables.POST_TYPE).append(" set delFlag = 1 ,updateUser ='")
                    .append(user.getUsername()).append("' where delFlag =0 and id =").append(infoDB.getId());
            try {
                UserDaoSql.getInstance().executeBySql(sql.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult(e.getMessage());
            }

            // cache
            PostTypeMemory.getInstance().delete(infoDB.getId());
            // 数据通讯
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POST_TYPE_INFO_DELETE).setLong01(infoDB.getId());
            JmsSender.setTopicSender(notify);

            return AjaxResult.successResult();
        }
        StringBuilder sql = new StringBuilder();
        sql.append("update  ").append(PojoTables.POST_TYPE).append(" set updateUser ='")
                .append(user.getUsername()).append("'")
                .append(", ").append("name ='").append(StringUtils.isNotEmpty(info.getName()) ? info.getName() : "").append("'")
                .append(", special =").append("'").append(StringUtils.isNotEmpty(info.getSpecial()) ? info.getSpecial() : "").append("'")
                .append(", note =").append("'").append(StringUtils.isNotEmpty(info.getNote()) ? info.getNote() : "").append("'")
                .append(", openState =").append(info.getOpenState())
                .append(" where delFlag =0 and id =").append(infoDB.getId());
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("操作失败,请稍后重试");
        }
        // modify
        PostTypeMemory.getInstance().modify(info.getId());
        // 数据通讯
        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POST_TYPE_INFO_DELETE).setLong01(info.getId());
        JmsSender.setTopicSender(notify);
        return AjaxResult.successResult();
    }

    // 切换是否打开
    @RequestMapping(value = "post/type/open-state/modify")
    public String post_type_modify(String token, Long id, Integer openState) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (openState == null || (openState != POST_TYPE_OPEN_STATE.OPEN && openState != POST_TYPE_OPEN_STATE.CLOSE)) {
            return AjaxResult.errorResult("请选择操作类型");
        }
        if (id != null && id > 0) {
            PostTypeInfo info = PostTypeMemory.getInstance().getById(id);
            if (info == null) {
                return AjaxResult.errorResult("未获取到类型信息");
            }
            if (info.getOpenState() == openState) {
                return AjaxResult.successResult();
            }
            StringBuilder sql = new StringBuilder();
            sql.append(" update ").append(PojoTables.POST_TYPE)
                    .append(" set ").append(" updateUser ='").append(user.getUsername()).append("'")
                    .append(" , openState = ").append(openState).append(" where id = ").append(id).append(" and ")
                    .append(" delFlag = 0");
            try {
                UserDaoSql.getInstance().executeBySql(sql.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult("操作失败,请稍后重试");
            }
            PostTypeMemory.getInstance().modify(id);
            // 数据通讯
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POST_TYPE_INFO_DELETE).setLong01(id);
            JmsSender.setTopicSender(notify);
        } else {
            StringBuilder sql = new StringBuilder();
            sql.append(" update ").append(PojoTables.POST_TYPE)
                    .append(" set ").append(" updateUser ='").append(user.getUsername()).append("'")
                    .append(" , openState = ").append(openState).append(" where ")
                    .append(" delFlag = 0");
            try {
                UserDaoSql.getInstance().executeBySql(sql.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult("操作失败,请稍后重试");
            }
            PostTypeMemory.getInstance().run();
        }
        return AjaxResult.successResult();
    }


    // list
    @RequestMapping(value = "/post/type/list")
    public String list(Integer openState) {
        List<PostTypeInfo> list = PostTypeMemory.getInstance().list(openState);
        if (list != null && !list.isEmpty()) {
            List<PostTypeInfoVO> resultList = new ArrayList<>();
            for (PostTypeInfo info : list) {
                PostTypeInfoVO infoVO = new PostTypeInfoVO();
                try {
                    ObjectUtils.copyPropertiesExclude(info, infoVO);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                resultList.add(infoVO);
            }
            return AjaxResult.successResult(resultList, resultList.size());
        }
        return AjaxResult.successResult();
    }

}
